using UnityEngine;
using System.Collections;

public class EffectsManager : MonoBehaviour
{
	private GameManager m_gameManager;
    private Transform m_panel;

	protected Object destructionParticles
	{
		get
		{
			if (m_destructionParticles = null)
			{
				m_destructionParticles = Resources.Load("gemExplosion");
			}
			return m_destructionParticles;
		}
	}

	private Object m_destructionParticles = null;

	public void Init(GameManager gameManager)
	{
		if (gameManager == null)
		{
			Debug.LogException(new System.Exception("Receiver GameManager reference is null!"));
		}
		m_gameManager = gameManager;
        m_panel = m_gameManager.mainPanel;
	}

	public void CreateExplosionEffect(Vector3 position, int playerID, int gemType)
	{
		if (m_gameManager == null)
		{
			Debug.LogError("This object is not initialized, yet used!");
			return;
		}
		GameObject particles = Instantiate(Resources.Load("gemExplosion")) as GameObject;
		if (particles == null)
		{
			Debug.LogWarning("Could not create particles!");
			return;
		}
		particles.transform.position = position;
		var partSystem = particles.GetComponent<ParticleSystem>();
		if (Data.multiplayer == true)
		{
			var info = m_gameManager.mainPlayer.GetPlayerInfo(playerID);
            if (info != null)
		        partSystem.startColor = info.color;
		}
		else
		{
            partSystem.startColor = m_gameManager.mainXMLGems.GetColor(gemType);
		}
		Destroy(particles, partSystem.duration);
	}

    public void CreateFloatingText(Vector3 position, string text, Color color, float duration, float shiftY = 0.07f)
    {
        GameObject textObj = Instantiate(Resources.Load("floatingText")) as GameObject;
        textObj.transform.parent = m_panel;

        var tweenAlpha = textObj.GetComponent<TweenAlpha>();
        tweenAlpha.duration = duration;

        var tweenPosition = textObj.GetComponent<TweenPosition>();
        tweenPosition.duration = duration;
        tweenPosition.from = new Vector3(position.x, position.y - shiftY * 0.5f, position.z);
        tweenPosition.to = new Vector3(position.x, position.y + shiftY * 0.5f, position.z);

        var label = textObj.GetComponent<UILabel>();
        label.depth = 5;
        label.text = text;
        label.color = color;

        Destroy(textObj, duration);
    }
	// Use this for initialization
	void Start()
	{
	
	}
	// Update is called once per frame
	void Update()
	{
	
	}
}

